package com.lzy.algorithm;

public class 有序链表合并 {

    public static void main(String[] args) {
        ListNode l1 = new ListNode(7);
        l1.next = new ListNode(33);
        l1.next.next = new ListNode(56);

        ListNode l2 = new ListNode(6);
        l2.next = new ListNode(23);
        l2.next.next = new ListNode(88);
        l2.next.next.next = new ListNode(101);

        ListNode result = mergeNode(l1, l2);

        while (result != null) {
            System.out.println(result.val);
            result = result.next;
        }
    }

    public static ListNode mergeNode(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(-1);
        ListNode node = head;
        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                node.next = l1;
                l1 = l1.next;
            } else {
                node.next = l2;
                l2 = l2.next;
            }
            node = node.next;
        }

        node.next = l1 == null ? l2 : l1;

        return head.next;
    }
}
